package edu.ynu.se.xiecheng.controller;

import edu.ynu.se.xiecheng.entity.Greenhouse;
import edu.ynu.se.xiecheng.service.GreenhouseService;
import edu.ynu.se.xiecheng.dto.GreenhouseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Map;

/**
 * 大棚信息控制器
 */
@RestController
@RequestMapping("/api/greenhouses")
@CrossOrigin
public class GreenhouseController {
    @Autowired
    private GreenhouseService greenhouseService;

    /**
     * 获取所有大棚列表
     */
    @GetMapping
    public ResponseEntity<List<Greenhouse>> getAllGreenhouses() {
        return ResponseEntity.ok(greenhouseService.findAll());
    }

//    /**
//     * 根据ID获取大棚信息
//     */
//    @GetMapping("/{id}")
//    public ResponseEntity<GreenhouseDTO> getGreenhouseById(@PathVariable Long id) {
//        return ResponseEntity.ok(greenhouseService.findByIdWithDevices(id));
//    }
//
    /**
     * 获取大棚详细信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getGreenhouseDetail(@PathVariable Long id) {
        try {
            return ResponseEntity.ok(greenhouseService.getGreenhouseDetail(id));
        } catch (EntityNotFoundException e) {
            return ResponseEntity.notFound().build();
        }
    }
} 